﻿using DrawTools.Serialize;
using DrawTools.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace DrawTools.Tools
{
    /// <summary>
    /// 闭合线条:含面积
    /// </summary>
    [SerializerType(typeof(DrawAreaClosedLineSerializer))]
    public sealed class AreaClosedLineDrawTool : DrawGeometryBase
    {

        #region 字段

        private List<Point> points = new List<Point>();

        double area;

        #endregion 字段

        /// <summary>
        /// 获取面积的值
        /// </summary>
        /// <returns></returns>
        public double GetValue() => area;

        public AreaClosedLineDrawTool(DrawingCanvas drawingCanvas) : base(drawingCanvas)
        {
            DrawingToolType = DrawToolType.AreaClosedLine;

            // 准备要处理的事件
            CanTouchDown = true;
        }

        protected override void OnDrawing(DrawingContext dc)
        {
            base.OnDrawing(dc);
            area = geometry.GetArea();

            if (area != 0)
            {
                area = area / Dpi.Cm2Wpf / Dpi.Cm2Wpf * 100;
                Text = area.ToString("0.00") + "mm²";

                if (!DrawerConfig.Instance.TextShowTogether)
                {
                    var formattedText = GetFormattedText();
                    var width = Text.Length * FontSize / 2;
                    Point textPoint = new Point();
                    textPoint.X = (points.Sum(q => q.X)) / (points.Count + 1) - width / 2;
                    textPoint.Y = (points.Sum(q => q.Y)) / (points.Count + 1) - FontSize / 2;
                    var textGeometry = formattedText.BuildGeometry(textPoint);
                    textGeometry.Transform = geometry.Transform;
                    dc.DrawGeometry(Pen.Brush, null, textGeometry);
                }
            }
        }

        #region 鼠标键盘事件

        public override bool OnTouchDown(int touchId, Point point)
        {
            TouchId = touchId;

            drawingCanvas.AddWorkingDrawTool(this);
            drawingCanvas.AddVisual(this);
            drawingCanvas.handleCursor = true;

            CanTouchDown = false;
            points.Add(point);
            geometry = new PathGeometry();
            var figure = new PathFigure { StartPoint = point };
            pathGeometry.Figures.Add(figure);

            CanTouchMove = true;
            CanTouchUp = true;

            if (TouchId != 0 || !drawingCanvas.CaptureMouse()) CanTouchLeave = true;
            return true;
        }

        public override bool OnTouchMove(Point point)
        {
            if ((point - points.Last()).Length < Pen.Thickness) return true;

            points.Add(point);

            var figure = pathGeometry.Figures[0];
            var line = new LineSegment(point, IsStroked) { IsSmoothJoin = true };
            figure.Segments.Add(line);

            var dc = RenderOpen();
            dc.DrawGeometry(null, Pen, geometry);
            dc.Close();

            return true;
        }

        public override bool OnTouchUp(Point point)
        {
            if (points.Count < 2) drawingCanvas.DeleteVisual(this);
            else
            {
                var first = points[0];
                var figure = pathGeometry.Figures[0];
                var line = new LineSegment(first, IsStroked) { IsSmoothJoin = true };
                figure.Segments.Add(line);

                //CombineAreaGeometry();
                Draw();
            }

            drawingCanvas.DeleteWorkingDrawTool(this);

            IsFinish = true;

            CanTouchMove = false;
            CanTouchUp = false;
            CanTouchLeave = false;

            if (TouchId == 0 && drawingCanvas.IsMouseCaptured)
                drawingCanvas.ReleaseMouseCapture();

            drawingCanvas.handleCursor = false;

            return true;
        }
        #endregion 鼠标键盘事件
    }
}